package com.twu.biblioteca.command;

import com.twu.biblioteca.book.Book;
import com.twu.biblioteca.Library;
import com.twu.biblioteca.exception.NoSuchBookException;

import java.io.IOException;

public class ReserveCommand implements Command, NeedLogin {
    public static final String SUCESS_TIP = "Thank You! Enjoy the book.";
    public static final String FAIL_TIP = NoSuchBookException.LACK_BOOK_TIP;
    public static final String INPUT_BOOK_ID = "Please input the id of book!";

    public void reserve(Book book, Library library) {
        boolean isAvailable = library.getBookManager().isBookAvailable(book.getId());
        if (isAvailable) {
            book.setReserve(true);
            library.showMessage(SUCESS_TIP);
        } else {
            library.showMessage(FAIL_TIP);
        }
    }

    @Override
    public void execute(Library library) throws IOException, NoSuchBookException {
        library.showMessage(INPUT_BOOK_ID);
        String id = library.getMessage();
        Book book = library.getBookManager().getBookById(Integer.valueOf(id));
        reserve(book , library);
    }

}
